

'''

求差异相关系数网络
输入:
    *name :

'''
import pandas as pd
import numpy as np
import random

# 用于真实数据
def getDiffMatrix_1(load1, load2, delta):

    Case = pd.read_excel(load1)
    Healthy = pd.read_excel(load2)

    namelist = Case.columns.values  # 列的名字  没有映射错误

    Cpd = Case.corr() - Healthy.corr()
    matrixC = Cpd.values

    for i in range(len(matrixC)):
        for j in range(len(matrixC)):
            if abs(matrixC[i, j]) < delta:
                matrixC[i, j] = 0
            else:
                matrixC[i, j] = 1

    E = []  # 收集C_diff中非零的坐标(i,j),未验证显著性的差异边集合
    matrixC_triu = np.triu(matrixC)  # 将对称矩阵转为上三角矩阵

    for i in range(len(matrixC)):
        for j in range(len(matrixC)):
            if matrixC_triu[i, j] != 0:
                E.append((i, j))
    E_name = []
    for i in E:
        E_name.append((namelist[i[0]],namelist[i[1]]))



    return matrixC, E,E_name



# 用于显著性检验
def getDiffMatrix_2(Case, Healthy, delta):


    namelist = Case.columns.values   # 列的名字   映射正确

    Cpd = Case.corr() - Healthy.corr()
    matrixC = Cpd.values

    for i in range(len(matrixC)):
        for j in range(len(matrixC)):
            if abs(matrixC[i, j]) < delta:
                matrixC[i, j] = 0
            else:
                matrixC[i, j] = 1

    E = []  # 收集C_diff中非零的坐标(i,j),未验证显著性的差异边集合
    matrixC_triu = np.triu(matrixC)  # 将对称矩阵转为上三角矩阵

    for i in range(len(matrixC)):
        for j in range(len(matrixC)):
            if matrixC_triu[i, j] != 0:
                E.append((i, j))
    E_name = []
    for i in E:
        E_name.append((namelist[i[0]],namelist[i[1]]))



    return matrixC, E,E_name

def getDiffMatrix_3(Case, Healthy, delta):


    namelist = Case.columns.values

    Cpd = Case.corr() - Healthy.corr()
    matrixC = Cpd.values

    edge_value = []
    edge_index = []

    matrixC_triu = np.triu(matrixC)  # 将对称矩阵转为上三角矩阵

    for i in range(len(matrixC)):
        for j in range(len(matrixC)):
            if abs(matrixC_triu[i, j]) >delta :
                edge_index.append((i,j))
                edge_value.append(abs(matrixC_triu[i,j]))


    edge_zip = zip(*sorted(zip(edge_value, edge_index), reverse=True))


    #print(list(edge_zip))
    return matrixC,list(edge_zip),namelist



#  用于performance性能验证
def getDiffMatrix_4(load1, load2, delta,pro):

    Case1 = pd.read_excel(load1)
    Healthy1 = pd.read_excel(load2)

    namelist1 = Case1.columns.values  # 列的名字  没有映射错误

    u = random.sample(range(0, len(namelist1)), pro);
    uname = [namelist1[i] for i in u]

    Case = Case1.loc[:,uname]
    Healthy = Healthy1.loc[:,uname]

    print(Case)
    print(Healthy)
    namelist = Case.columns.values  # 列的名字  没有映射错误

    Cpd = Case.corr() - Healthy.corr()
    matrixC = Cpd.values

    for i in range(len(matrixC)):
        for j in range(len(matrixC)):
            if abs(matrixC[i, j]) < delta:
                matrixC[i, j] = 0
            else:
                matrixC[i, j] = 1

    E = []  # 收集C_diff中非零的坐标(i,j),未验证显著性的差异边集合
    matrixC_triu = np.triu(matrixC)  # 将对称矩阵转为上三角矩阵

    for i in range(len(matrixC)):
        for j in range(len(matrixC)):
            if matrixC_triu[i, j] != 0:
                E.append((i, j))
    E_name = []
    for i in E:
        E_name.append((namelist[i[0]],namelist[i[1]]))



    return matrixC, E,E_name